import { Vec3 } from 'cc';

const _vector1 = /*@__PURE__*/ new Vec3();
const _vector2 = /*@__PURE__*/ new Vec3();

/* 移植自Three.js */
export class Plane {

	constructor(public normal = new Vec3( 1, 0, 0 ),public constant = 0 ) {
		// normal is assumed to be normalized
		this.normal = normal;
		this.constant = constant;
	}
	
    setFromNormalAndCoplanarPoint( normal:Vec3, point:Vec3 ):Plane {
		this.normal.set(normal);
		this.constant = - point.dot( this.normal );
		return this;

	}
    setFromCoplanarPoints(a:Vec3,b:Vec3,c:Vec3):Plane{

        const normal = Vec3.subtract(_vector1,c,b).cross(Vec3.subtract(_vector2,a,b)).normalize()

		// Q: should an error be thrown if normal is zero (e.g. degenerate plane)?

		this.setFromNormalAndCoplanarPoint( normal, a );

		return this;

    }
    projectPoint(point:Vec3, target:Vec3 ):Vec3{
        return target.set(this.normal.multiplyScalar( - this.distanceToPoint( point ) ).add( point ));
    }
    distanceToPoint(point:Vec3 ):Number{
		return this.normal.dot( point ) + this.constant;
	}

}


